package com.cndw.kungfu.model.battle.buff;

import com.cndw.kungfu.domain.BuffMode;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.model.battle.Battle;
import com.cndw.kungfu.model.battle.Fighter;


/**
 * buff模块.多态模式,自动装载
 * @author fantadust
 * @date 2011-5-4 下午05:04:03
 */
abstract class BuffAbstract implements BuffLibI {
	/**
	 * buff模型
	 */
	protected BuffMode buffMode;
	/**
	 * 施法者对象
	 */
	protected Fighter source;
	/**
	 * 接受者对象
	 */
	protected Fighter target;

	protected Battle battle;
	
	protected int buffInitRound;
	
	protected int signId = 0;
	
	/**
	 * 初始执行buff
	 */
	abstract public void initTask();
	/**
	 * 結束buff
	 */
	abstract public void endTask();
	

	
	
	
	@Override
	public BuffLibI initBuffMode(BuffMode buffMode,Fighter source,Fighter target,Battle battle){
		this.buffMode = buffMode;
		
		this.source = source;		
		this.target = target;
		
		this.battle = battle;			
		buffInitRound = battle.getRound();	
		
		signId = buffMode.getLibId();
		
		if (target.getBuffMap().containsKey(signId)) {
			target.getBuffMap().get(signId).cancel();			
		}		
		target.getBuffMap().put(signId, this);
		
		initTask();			
		LogUtil.getMain().info("init one  buff");	
		
		return this;
	}	


	@Override
	public void initLifeTask(){	
		
	}
	
	@Override
	public final boolean cancel(){
		onCancel();//取消時候觸發
		endTask();//結束加成
		onOver();//刪除		
		return true;
	}
	
	@Override
	public void onCancel(){
		
	}
	
	@Override
	public final void onOver(){		
		if (target == null ) {
			return ;
		}
		target.getBuffMap().remove(buffMode.getId());	
		LogUtil.getMain().debug("結束buff");
	}

	
	@Override
	public BuffMode getBuffMode() {
		return buffMode;
	}


	@Override
	public Fighter getSource() {
		return source;
	}
	@Override
	public Fighter getTarget() {
		return target;
	}
	@Override
	public int getBuffInitRound() {
		return buffInitRound;
	}
	@Override
	public boolean checkOver() {		
		if (buffMode.getLifeTime() > 0 && buffMode.getLifeTime() <= battle.getRound() - buffInitRound ) {
			return cancel();
		}
		return false;
	}
	@Override
	public int getSignId() {
		return signId;
	}
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		LogUtil.getMain().debug("buff finalize");
		
	}
	
	
	
	
	
}
